﻿using Microsoft.AspNetCore.Mvc;
using oldislandapi.IService;

namespace oldislandapi.Controllers
{
    [Route("api/classic")]
    [ApiController]
    public class ClassicController : ControllerBase
    {
        private IClassicService _classicService;
        private IFavorService _favorService;
        public ClassicController(IClassicService classicService,IFavorService favorService)
        {
            _classicService = classicService ?? throw new ArgumentNullException(nameof(classicService));
            _favorService = favorService ?? throw new ArgumentNullException(nameof(favorService));
        }

        /// <summary>
        /// 获取最新一期
        /// </summary>
        /// <returns></returns>
        [HttpGet("latest")]
        public async Task<ActionResult> GetLastest() => Ok(await _classicService.GetSingleDataAsync(x => true));

        /// <summary>
        /// 获取当前一期的下一期
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpGet("{index}/next", Name = nameof(GetLastestNext))]
        public async Task<ActionResult> GetLastestNext(int index) => Ok(await _classicService.GetSingleDataAsync(x => x.Index == index + 1));

        /// <summary>
        /// 获取某一期详细信息
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpGet("{type}/{id}", Name = nameof(GetDetails))]
        public async Task<ActionResult> GetDetails(int type, int id) => Ok(await _classicService.GetSingleDataAsync(x => x.Type == type && x.ID == id));

        /// <summary>
        /// 获取当前一期的上一期
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpGet("{index}/previous", Name = nameof(GetPrevious))]
        public async Task<ActionResult> GetPrevious(int index)
        {
            var value = index > 1 ? index - 1 : index;
            return Ok(await _classicService.GetSingleDataAsync(x => x.Index == value));
        }

        /// <summary>
        /// 获取点赞信息
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpGet("{type}/{id}/favor", Name = nameof(GetFavor))]
        public async Task<ActionResult> GetFavor(int type, int id) => Ok(await _classicService.GetSingleDataAsync(x => x.Type == type && x.ID == id));

        /// <summary>
        /// 获取我喜欢的期刊
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        [HttpGet("favor")]
        public async Task<ActionResult> GetFavor() => Ok(await _favorService.GetSingleDataAsync(x => true));
    }
}
